package com.xpgk.mvc.bturnClass;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.buyProduct.BuyProductService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.measurement.Temporary;
import com.xpgk.mvc.measurement.TemporaryServices;
import com.xpgk.mvc.productPrice.ProductPrice;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSales.ProductSalesOrder;
import com.xpgk.tools.ToolRandoms;

@Service(name = BturnClassService.serviceName)
public class BturnClassService  extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BturnClassService.class);
	public static final String serviceName = "bturnClassService";	
	
	/**
	 * 根据顾客ids查出已经购买的疗程
	 * */
	public  Map<String, Object>  getLcByCus(String cusId,String orgId)
	{	
		Map<String, Object> mm=new HashMap<>();
		String  sql=getSqlMy(CustomerProduct.sqlId_selByCusAndType);
		List<CustomerProduct>  cpList=CustomerProduct.dao.find(sql,cusId,Code.lc_ids,orgId);
		String msg="";
		if(cpList != null && cpList.size()>0)
		{	
//			mm.put("msg", Customer.column_success);
			msg=ConstantRender.render_success_code;
			//如果是单疗程
			if(cpList.get(0).getStr("proUnits").equals(Code.dlc_units)||(cpList.get(0).getStr("name").equals("标准疗程")&&cpList.get(0).getInt("number")==1)){
				mm.put("customerProduct", cpList.get(0));
				Customer  cus = Customer.dao.findById(cusId);
				int   treatment = TemporaryServices.getTreatment(cus.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));
				if(treatment==1)
				{
					//一疗程
					List<Product>  pList=Product.dao.getYiLiaoCheng(Code.bzlc_units, Code.ylc_capacity, orgId);
					mm.put("newlc", pList.get(0));
					mm.put("newTotal", treatment*pList.get(0).getFloat(ProductPrice.column_outPrice));
				}else
				{
					//标准疗程
					List<Product>  pList=Product.dao.getLiaoCheng(Code.bzlc_units, Code.ylc_capacity, orgId);
					mm.put("newlc", pList.get(0));
					mm.put("newTotal", treatment*pList.get(0).getFloat(ProductPrice.column_outPrice));
				}
				mm.put("newNumber", treatment);
				//查询是否有欠款
				List<ProductSalesOrder>  psoList=ProductSalesOrder.dao.getProductSalesOrders(cusId, orgId);
				if(psoList!=null && psoList.size()>0)
				{
					msg="该顾客有欠款未还清！";
				}
			}
			/*else if(cpList.get(0).getStr(CustomerProduct.column_productID).equals("0896ce4d4970455d9184e6dd1ced6a0e")&&cpList.get(0).getInt(CustomerProduct.column_count)==1){
				mm.put("customerProduct", cpList.get(0));
				Customer  cus = Customer.dao.findById(cusId);
				int   treatment = TemporaryServices.getTreatment(cus.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));
				if(treatment==1)
				{
					//一疗程
					List<Product>  pList=Product.dao.getYiLiaoCheng(Code.bzlc_units, Code.ylc_capacity, orgId);
					mm.put("newlc", pList.get(0));
					mm.put("newTotal", treatment*pList.get(0).getFloat(ProductPrice.column_outPrice));
				}
				mm.put("newNumber", treatment);
				//查询是否有欠款
				List<ProductSalesOrder>  psoList=ProductSalesOrder.dao.getProductSalesOrders(cusId, orgId);
				if(psoList!=null && psoList.size()>0)
				{
					msg="该顾客有欠款未还清！";
				}
			}*/
			
			else{
				msg="顾客所购疗程为'"+cpList.get(0).getStr("name")+"',不能转其他疗程！";
			}
		}
		else
		{	
			msg="顾客还未购买疗程产品";
		}
		mm.put("msg", msg);
		return mm;
	}
	
	/**疗程转型结算
	 * 
	 * @param p
	 * @param c
	 * @param ps
	 * @param pso
	 * @param orgIds
	 * @param userIds
	 */
	public  String  jiesuan(Product p,Customer c,ProductSales ps,ProductSalesOrder pso,String orgIds,String userIds,String oldProIds)
	{	
		String  msg=ConstantRender.render_success_code;
		//禁用顾客转型前疗程
		String cpSql=getSqlMy(CustomerProduct.sqlId_selByCusAndPro);
		CustomerProduct  cp=CustomerProduct.dao.findFirst(cpSql,c.getStr(Customer.column_ids),oldProIds,orgIds);
		if(cp!=null)
		{
			cp.set(CustomerProduct.column_status, "0");
			if(!cp.update())
			{
				log.debug("禁用顾客旧疗程失败");
				throw new RuntimeException("禁用顾客旧疗程失败!");
			}
		}
		
		Product  pp=Product.dao.findById(p.getStr(Product.column_ids));
		//保存新的顾客疗程
		CustomerProduct  ccpp=new CustomerProduct();
		ccpp.set(CustomerProduct.column_ids, ToolRandoms.getUuid(true))
			.set(CustomerProduct.column_customerID, c.getStr(Customer.column_ids))
			.set(CustomerProduct.column_productID, p.getStr(Product.column_ids))
			.set(CustomerProduct.column_organids, orgIds)
			.set(CustomerProduct.column_count, ps.getInt(ProductSales.column_number))
			.set(CustomerProduct.column_proUnitPrice, ps.getFloat(ProductSales.column_unitPrice))
			.set(CustomerProduct.column_capacity, pp.getStr(Product.column_proSpecification))
			.set(CustomerProduct.column_proExperience, pp.getStr(Product.column_proExperience));
		if(pp.getStr(Product.column_proExperience)!=null&&!pp.getStr(Product.column_proExperience).equals("")){
			ccpp.set(CustomerProduct.column_totalExperience, ps.getInt(ProductSales.column_number)*Float.parseFloat(pp.getStr(Product.column_proExperience)));
		}
			
		ccpp.set(CustomerProduct.column_capacities,pp.getStr(Product.column_proCapacity))
			.set(CustomerProduct.column_createDate, new Date());
		if(!ccpp.save())
		{
			log.debug("添加顾客新疗程失败");
			throw new RuntimeException("添加顾客新疗程失败!");
		}
		//保存新的顾客协议
		String  caSql=getSqlMy(CustomerAgreement.sqlId_selByCusAndOrg);
		CustomerAgreement  ca=CustomerAgreement.dao.findFirst(caSql, c.getStr(Customer.column_ids),orgIds);
		if(ca!=null)
		{
			ca.set(CustomerAgreement.column_codeIds, p.getStr(Product.column_ids))
				.set(CustomerAgreement.column_treatment, ps.getInt(ProductSales.column_number))
				.set(CustomerAgreement.column_time, new Date());
			if(!ca.update())
			{
				log.debug("更新顾客协议失败");
				throw new RuntimeException("更新顾客协议失败!");
			}
		}
		
		//保存订单信息
		
		String number=BuyProductService.getOrderNumber(orgIds);//获取新的订单编号
		pso.set(ProductSalesOrder.column_ids, ToolRandoms.getUuid(true))
			.set(ProductSalesOrder.column_orderNumber, number)
			.set(ProductSalesOrder.column_organIds, orgIds)
			.set(ProductSalesOrder.column_customerIds, c.getStr(Customer.column_ids))
			.set(ProductSalesOrder.column_userIds, userIds)
			.set(ProductSalesOrder.column_date, new Date());
		if(!pso.save())
		{
			log.debug("保存新订单失败");
			throw new RuntimeException("保存新订单失败!");
		}
		//保存店面产品销售记录
		ps.set(ProductSales.column_ids, ToolRandoms.getUuid(true))
			.set(ProductSales.column_organids, orgIds)
			.set(ProductSales.column_orderIds, pso.getStr(ProductSalesOrder.column_ids))
			.set(ProductSales.column_type, pp.getStr(Product.column_typeIds))
			.set(ProductSales.column_productID, p.getStr(Product.column_ids))
			.set(ProductSales.column_customerID, c.getStr(Customer.column_ids))
			.set(ProductSales.column_userID, userIds)
			.set(ProductSales.column_date, new Date())
			.set(ProductSales.column_consumerType, Code.xflb_zx);
		if(!ps.save())
		{
			log.debug("保存销售记录失败");
			throw new RuntimeException("保存销售记录失败!");
		}
		return  msg;
	}
	
}